package com.example.test.controller;

import com.example.test.model.Student;
import com.example.test.service.StudentService;
import com.example.test.util.Pagination;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("student")
public class StudentController extends BaseController {

    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    //    @RequestMapping(value = "create", method = RequestMethod.POST)
    @PostMapping("create")
    private String create(Student student) {
        studentService.create(student);
        return "redirect:/student/queryAll/1";
    }

    @GetMapping("queryAll/{currentPage}")
    private String queryAll(@PathVariable("currentPage") int currentPage) {
        Pagination<Student> pagination = studentService.queryAll(currentPage);
        session.setAttribute("pagination", pagination);
        return "redirect:/student/list.jsp";
    }


    @GetMapping("queryById/{id}")
    private String queryById(@PathVariable("id") int id) {
        session.setAttribute("student", studentService.queryById(id));
        return "redirect:/student/edit.jsp";
    }

    @PostMapping("modify")
    private String modify(Student student) {
        studentService.modify(student);
        return "redirect:/student/queryAll";
    }
    @GetMapping("removeById/{id}")
    private String removeById(@PathVariable("id") int id) {
        studentService.removeById(id);
        return "redirect:/student/queryAll";
    }
}
